{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 图像分割",
   "id": "1d285a1d630d5a02"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 转置卷积",
   "id": "d414375e487f85ce"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-07T14:08:23.072934Z",
     "start_time": "2025-05-07T14:08:23.051386Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "import numpy as np\n",
    "input_feat = torch.tensor([[[[1, 2], [3, 4]]]], dtype=torch.float32) # 输入特征图\n",
    "input_feat"
   ],
   "id": "f2b6b8fff480b176",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[[1., 2.],\n",
       "          [3., 4.]]]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-07T14:08:25.956468Z",
     "start_time": "2025-05-07T14:08:25.938433Z"
    }
   },
   "cell_type": "code",
   "source": [
    "kernels = torch.tensor([[[[1, 0], [1, 1]]]], dtype=torch.float32) # 卷积核\n",
    "kernels"
   ],
   "id": "c25bd980dd780179",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[[1., 0.],\n",
       "          [1., 1.]]]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-07T14:11:40.433939Z",
     "start_time": "2025-05-07T14:11:40.382933Z"
    }
   },
   "cell_type": "code",
   "source": [
    "convTrans = nn.ConvTranspose2d(1, 1, kernel_size=2, stride=1, padding=0, bias = False)  # 转职卷积核\n",
    "convTrans.weight=nn.Parameter(kernels)\n",
    "convTrans(input_feat) # 结果与补0后运算一致"
   ],
   "id": "bea5a0908735a2bf",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[[1., 2., 0.],\n",
       "          [4., 7., 2.],\n",
       "          [3., 7., 4.]]]], grad_fn=<ConvolutionBackward0>)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
